package com.model;

import java.sql.SQLException;
import java.util.*;
import java.sql.*;
import com.javabean.*;

public class SearchDocByWhich {
	public Vector searchDocByOwner(String userName){
		Vector v = new Vector();
		
		try{
			Connection con = DataBase.getConnection();
			Statement stat = con.createStatement();
			
			String sql = "SELECT docId,docName,userName,docDateOfUp,docType FROM docmansysdb.doc,docmansysdb.user WHERE userId = docOwner AND userName like '%" + userName + "%' ORDER BY docDateOfUp";
			ResultSet rs = stat.executeQuery(sql);
			
			while(rs.next()){
				Doc doc = new Doc();
				doc.setDocId(rs.getLong("docId"));
				doc.setDocName(rs.getString("docName"));
				doc.setDocOwner(rs.getString("userName"));
				doc.setDocDateOfUp(rs.getString("docDateOfUp"));
				doc.setDocType(rs.getString("docType"));
				
				v.add(doc);
			}
			
			rs.close();
			con.close();
		}catch(SQLException e){
			e.printStackTrace();
			return null;
		}catch(Exception e2){
			e2.printStackTrace();
			return null;
		}
		return v;
	}
	
	public Vector searchDocByDocName(String docName){
		Vector v = new Vector();
		
		try{
			Connection con = DataBase.getConnection();
			Statement stat = con.createStatement();
			
			String sql = "SELECT docId,docName,userName,docDateOfUp,docType FROM docmansysdb.doc,docmansysdb.user WHERE userId = docOwner AND docName LIKE '%" + docName + "%' ORDER BY docDateOfUp";
			//System.out.println(sql);
			ResultSet rs = stat.executeQuery(sql);
			
			while(rs.next()){
				Doc doc = new Doc();
				doc.setDocId(rs.getLong("docId"));
				doc.setDocName(rs.getString("docName"));
				doc.setDocOwner(rs.getString("userName"));
				doc.setDocDateOfUp(rs.getString("docDateOfUp"));
				doc.setDocType(rs.getString("docType"));
				
				v.add(doc);
			}
			
			rs.close();
			con.close();
		}catch(SQLException e){
			e.printStackTrace();
			return null;
		}catch(Exception e2){
			e2.printStackTrace();
			return null;
		}
		return v;
	}
}
